/**
 * Created by gdg on 2017/5/7.
 */
import React, {Component} from 'react'
import {
	View,
	TextInput,
	Button,
	AsyncStorage
} from 'react-native'
import {observer} from 'mobx-react/native'
//观察对象
@observer
class LoginScreen extends Component {
	static navigationOptions = {
		title: '登录'
	};

	constructor(props) {
		super(props);
		this.state = {username: '请输入手机号', password: '请输入密码'};
	}

	async submit() {
		const result = await this.props.screenProps.domainStore.login(this.state.username, this.state.password);
		if (result)
			await AsyncStorage.setItem('isLogin','true');
			await this.props.navigation.navigate('HomeScreen');
	}
	async componentDidMount(){
		//检查是否登录
		const isLogin = await AsyncStorage.getItem('isLogin');
		if(isLogin)
			this.props.navigation.navigate('HomeScreen');
	}
	render() {
		return (
			<View>
				<TextInput
					style={{height: 40, borderColor: 'gray', borderWidth: 1}}
					onChangeText={(text) => this.setState({username: text})}
					placeholder='请输入手机号'
				/>
				<TextInput
					style={{height: 40, borderColor: 'gray', borderWidth: 1}}
					onChangeText={(text) => this.setState({password: text})}
					placeholder='请输入密码'
				/>
				<Button title="提交" onPress={this.submit.bind(this)}/>
			</View>
		);
	}
}

export default LoginScreen;